<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in detailList">
					<uni-card :title="'序号:'+(index+1)">
						<text class="uni-body">物料编码：{{ item.itemCode }}</text><br>
						<text class="uni-body">数量：{{ item.codeEqualQty }}</text><br>
						<text class="uni-body">箱号：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>

		<view class="footer">
			<button type="default" @click="print">补打唛头签</button>
		</view>
	</view>
</template>

<script>
	import {
		getToken
	} from '@/utils/auth'
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				detailList: [],
				trojanCode: '',
				baseUrl: getApp().globalData.config.baseUrl,
				userName: this.$store.state.user.name,
				orgId: getApp().globalData.config.orgId,
				ip: getApp().globalData.config.ip,
				port: getApp().globalData.config.port,
				identification: '',
				soDocno: '', //销售订单
				sn: '', //拖序号
				custPoNo: '',
				sn1: '', //托序号2
				invoiceNo: '', //发票号

				qrcodeContent: '',
				printType: '',
				reprint: '',

				packageId: '',
				markstemplate: ''
			}
		},

		onShow() {
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},

		onHide: function() {
			this.unregisterScan();
		},

		destroyed: function() {
			this.unregisterScan();
		},

		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
				return false;
			}
			//that.unregisterScan();
			uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				delta: 1
			});
			return true;
		},

		created: function() {
			this.orgId = uni.getStorageSync('orgId');
			var ipValue = uni.getStorageSync('ip');
			if (ipValue != null && ipValue != '') {
				this.ip = ipValue;
			}
			var portValue = uni.getStorageSync('port');
			if (portValue != null && portValue != '') {
				this.port = portValue;
			}
		},

		methods: {
			initScan() {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
						let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描	
						var result = common.scanCode(codeStr);
						if (result[0] == "T") {
							_this.trojanCode = result[1];
							_this.checkFinished(_this.trojanCode);
						} else {
							uni.showToast({
								title: '请正确扫描托号',
								icon: 'error',
								duration: 3000
							});
						}
					}
				});
			},

			registerScan() {
				main.registerReceiver(receiver, filter);
			},

			unregisterScan() {
				main.unregisterReceiver(receiver);
			},

			//查询此托是否已完成
			checkFinished(codeStr) {
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;
				uni.request({
					url: this.baseUrl + '/system/unpline/pallet',
					data: {
						orgId: that.orgId,
						userName: that.userName,
						cipherCode: that.trojanCode,
						type: 'check'
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						console.log(res)
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							this.query(codeStr);
						} else if (res.data.code == '201' || res.data.code == 201) {
							uni.showToast({
								title: '此' + codeStr + '托未完成，不允许补打唛头签。',
								icon: 'none',
								duration: 3000
							});
							return;
						} else if (res.data.code == '50000') { //20240205判断组托计划锁定
							uni.showToast({
								duration: 4000,
								icon: 'none',
								title: '该组托计划正在做变更已被锁定,不允许操作!'
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})
			},

			query(codeStr) {
				uni.setNavigationBarTitle({
					title: codeStr
				});
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/record/boxRe',
					data: {
						qrcodeContent: codeStr,
						orgId: this.orgId
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						console.log(res)
						uni.hideLoading();
						if (res.data.code == '201' || res.data.code == 201) {
							console.log(res.data);
							this.detailList = res.data.data.returnList;
							this.soDocno = res.data.data.soDocno;
							this.sn = res.data.data.attr5;
							this.packageId = res.data.data.packageId;
							this.sn1 = res.data.data.attr6;
							this.invoiceNo = res.data.data.invoiceNo
							this.identification = res.data.data.identification;
							this.custPoNo = res.data.data.custPoNo;
							this.SaleOrder = res.data.data.attr6

							this.qrcodeContent = res.data.data.qrcodeContent;
							this.printType = res.data.data.printType;
							this.reprint = res.data.data.reprint;

							this.getcustom()

						} else if (res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '此' + codeStr + '托未校验，不允许补打唛头签。',
								icon: 'none',
								duration: 3000
							});
							return;
						} else {
							this.detailList = [];
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}

					},
				})
			},
			getcustom() {
				console.log(this.sn1)
				uni.request({
					url: this.baseUrl + '/system/templates/getMarksAndBoxCode',
					data: {

						orgId: this.orgId,
						packageId: this.packageId,
						panelCode: this.sn1

					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						console.log(res)
						this.markstemplate = res.data.data[0].markstemplate

					},
				})
			},

			print() {
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;
				// console.log(that.markstemplate)
				// console.log(that.userName)
				// console.log(that.sn1,)
				// console.log(that.custPoNo)
				// console.log(that.invoiceNo)
				// console.log(that.soDocno)
				// console.log(that.qrcodeContent)
				// console.log(that.printType)
				// console.log(that.reprint)
				var newtemplateCode = ''
				if (that.markstemplate == '' || that.markstemplate == null) {
					newtemplateCode = that.identification
				} else {
					newtemplateCode = that.markstemplate
				}

				uni.request({
					url: that.baseUrl + '/u9/printerOrder',
					data: {
						orgId: that.orgId,
						ip: that.ip + ":" + that.port,
						templateCode: newtemplateCode,
						userName: that.userName,
						panelNo: that.sn1,
						poNo: that.custPoNo,
						InvoiceNo: that.invoiceNo,
						SaleOrderNo: that.soDocno,

						qrcodeContent: that.qrcodeContent,
						printType: that.printType,
						reprint: that.reprint
					},
					method: 'POST',
					header: {

					},
					dataType: 'json',
					success: (res) => {
						console.log("query", res)
						//隐藏加载框
						uni.hideLoading();
						console.log('print', res.data);
						if (res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '补打唛头签成功',
								icon: 'none',
								duration: 3000
							});
							//that.unregisterScan();
							setTimeout(function() {
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							}, 1000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}

					},
				})
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}

	.text-content {
		font-size: 48rpx;
		text-align: center;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}

	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: 30px;
		font-size: 26rpx;
	}

	page {
		width: 100%;
		height: 100%;
	}


	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}

	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}

	.list-cont {
		flex: 1;
		position: relative;

		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}

		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;

		}
	}


	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>